package com.welsmann.app.start.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.welsmann.app.start.common.StartConst;
import com.welsmann.app.start.entity.News;
import com.welsmann.app.start.entity.NewsTitle;
import com.welsmann.app.start.service.INewsService;

@Controller
@RequestMapping("news")
public class NewsController extends BaseController{
	
	@Resource(name = "NewsService")
	private INewsService newsService;

	@RequestMapping("index")
	public ModelAndView index() {
		ModelAndView mav = new ModelAndView("news");
		mav.addObject(StartConst.REQUEST_NEWS_TYPE_LIST, newsService.getAllType());
		return mav;
	}
	
	@RequestMapping(value = "list/{tId}", method = RequestMethod.POST)
	public @ResponseBody String getNewsTitleList(@PathVariable String tId) {
		List<NewsTitle> lstTitle = newsService.getTitleListByTypeId(tId);
		return JSON.toJSONString(lstTitle);
	}
	
	@RequestMapping("view/{nId}")
	public ModelAndView getNewsById(@PathVariable String nId) {
		ModelAndView mav = new ModelAndView("news-content");
		NewsTitle title = newsService.getNewsTitleById(nId);
		mav.addObject(StartConst.REQUEST_NEWS_TITLE_OBJECT, title);
		News news = newsService.getNewsById(nId);
		mav.addObject(StartConst.REQUEST_NEWS_OBJECT, news);
		return mav;
	}
	

}
